<?php
class DudoanController extends Controller {
    
    public function actionIndex(){
        $date = date('Y-m-d',time());
        $data = array();
        $day   = DateUtils::convertThuToNumber();
        $province = Province::getRowsByDay($day);
        $data = AdminDudoanKetQua::getRowByDate($date); 
        $this->render('index',array("province"=>$province,"data"=>$data));
    }
    
    public function actionInsertrow(){
        $arr_boso   = isset($_POST['boso'])         ?$_POST['boso']         :'';
        $arr_name   = isset($_POST['province_name'])?$_POST['province_name']:'';
        $arr_id     = isset($_POST['province_id'])  ?$_POST['province_id']  :"";
        for($i=0;$i<count($arr_boso);$i++){
            $insert = AdminDudoanKetQua::insertRow($arr_name[$i],$arr_boso[$i],$arr_id[$i]);
            if($insert == 0){
                echo 0; 
                die;
            }
        }
        echo $insert;
    }
    
    public function actionUpdaterow(){
        //$delete =  AdminDudoanKetQua::deleteRowByDate("2012-03-12");
        //$insert  =  AdminDudoanKetQua::insertRowDate("Cà Mau","*CAU CM* Du doan KQXS Ca Mau 12/03 *Dau:XIU *Duoi TAI:05-17 LO 2 So:15-68-53 LO 3 So:942-030-755 *Chuc ban may man.",8,"2012-03-12");        
        $flag = 0;
        $arr_boso = isset($_POST['boso'])?$_POST['boso']:'';
        $arr_name = isset($_POST['province_name'])?$_POST['province_name']:''; 
        for($i=0;$i<count($arr_boso);$i++){
            $update = AdminDudoanKetQua::updateRow($arr_boso[$i],$arr_name[$i]);
            if($update > 0){
                $flag++;    
            }
        }
        echo $flag; 

    }
    
    public function actionLichsu(){
        $url = new Url();
        $arrDate = "";
        $from   = isset($_GET['from'])  ?$_GET['from']  :date('d-m-Y',time()-2592000);
        $to     = isset($_GET['to'])    ?$_GET['to']    :date('d-m-Y',time());
        
        $date_from  = date("Y-m-d",strtotime($from));
        $date_to    = date("Y-m-d",strtotime($to));
        
        $page   = isset($_GET['page'])  ?$_GET['page']  :1;

        $data = AdminDudoanKetQua::countRowInDate($date_from,$date_to);
        $count_data = count($data); 
        
        $rows_per_page = 10;
        
        if($count_data % $rows_per_page == 0){
            $total_page = floor($count_data/$rows_per_page);
        }else{
            $total_page = floor($count_data/$rows_per_page) + 1;
        }
        
        $begin = ($page - 1) * $rows_per_page;
        
        $date = AdminDudoanKetQua::getRowInDatePaging($date_from,$date_to,$begin,$rows_per_page);
        
        $row = array();
        for($i = 0; $i < count($date); $i++){
            $row[$i] = AdminDudoanKetQua::getRowByDate($date[$i]['date']);
        }
        $count_row = count($row);
        
        $get_url = "?from=".$from."&to=".$to;
        $page = Paging::showPageNavigation($page,$total_page,$url->createUrl("dudoan/lichsu").$get_url."&");
        $this->render('lich_su',array("page"=>$page,"row"=>$row,"count_row"=>$count_row,"date"=>$date));
    }
    
    public function actionDeleteRow(){
        $id = isset($_POST['id'])?$_POST['id']:"";
        if($id != ""){
            $delete =  AdminDudoanKetQua::deleteRowById($id);
            echo $delete;   
        }else{
            echo 0;
        }
    }
    
    public function actionDeleteRowByDate(){
        
        $date = isset($_POST['date'])?$_POST['date']:"";
        
        if($date != ""){
            $delete =  AdminDudoanKetQua::deleteRowByDate($date);
            echo $delete;   
        }else{
            echo 0;
        }
    }
    
}